package Queue;
import LinkList.ListNode;

import java.util.*;
public class Queue {
    public ListNode head;
    public ListNode tail;
    private int usedSize;
    public void offer(int val) {
        ListNode node=new ListNode(val);
        if(head==null) {
            head=tail=node;
        }else {
            tail.next=node;
            tail=node;
        }
        usedSize++;
    }
    public int poll() {
        if(empty()) {
            throw new QueueEmptyException("空队列无法删除!!!");
        }
        int val=head.data;
        head = head.next;
        if(head==null) {             //如果最后一个结点也删除了，那么last就要置为空，否则造成野指针的问题
            tail=null;
        }
        usedSize--;
        return val;
    }
    private boolean empty() {
        return usedSize==0;
    }
    public int peek() {            //获取队头的元素
        if(empty()) {
            throw new QueueEmptyException("空队列没有元素!!!");
        }
        return head.data;
    }
    public int size() {
        return usedSize;
    }
}
